<template>
  <el-dialog
    :title="!dataForm.orderNo ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单id" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="门票id" prop="ticketId">
      <el-input v-model="dataForm.ticketId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="游客证件编号" prop="visitorIdCard">
      <el-input v-model="dataForm.visitorIdCard" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="purchaseQuantity">
      <el-input v-model="dataForm.purchaseQuantity" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="总价格" prop="totalPrice">
      <el-input v-model="dataForm.totalPrice" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="是否完成" prop="isFinished">
      <el-input v-model="dataForm.isFinished" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="二维码地址" prop="qrCodeUrl">
    <el-input v-model="dataForm.qrCodeUrl" placeholder=""></el-input>
    </el-form-item>
    <!-- <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getOrderById, saveOrUpdateOrder } from '@/api/orders'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        orderNo: 0,
        orderId: '',
        ticketId: '',
        visitorIdCard: '',
        purchaseQuantity: '',
        totalPrice: '',
        isFinished: '',
        qrCodeUrl: '',
        createTime: '',
        updateTime: ''
      },
      dataRule: {
        orderId: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        ticketId: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        visitorIdCard: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        purchaseQuantity: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        totalPrice: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        isFinished: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        qrCodeUrl: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.orderNo = id || 0
      this.visible = true
      this.$nextTick(async () => {
        this.$refs.dataForm.resetFields()
        if (this.dataForm.orderNo) {
          const { data } = await getOrderById(this.dataForm.orderNo)
          if (data && data.code === 0) {
            this.dataForm.orderId = data.orders.orderId
            this.dataForm.ticketId = data.orders.ticketId
            this.dataForm.visitorIdCard = data.orders.visitorIdCard
            this.dataForm.purchaseQuantity = data.orders.purchaseQuantity
            this.dataForm.totalPrice = data.orders.totalPrice
            this.dataForm.isFinished = data.orders.isFinished
            this.dataForm.qrCodeUrl = data.orders.qrCodeUrl
            this.dataForm.createTime = data.orders.createTime
            this.dataForm.updateTime = data.orders.updateTime
          }

          // this.$http({
          //   url: `/orders/info/${this.dataForm.orderNo}`,
          //   method: 'get'
          // }).then(({ data }) => {
          //   if (data && data.code === 0) {
          //     this.dataForm.orderId = data.orders.orderId
          //     this.dataForm.ticketId = data.orders.ticketId
          //     this.dataForm.visitorIdCard = data.orders.visitorIdCard
          //     this.dataForm.purchaseQuantity = data.orders.purchaseQuantity
          //     this.dataForm.totalPrice = data.orders.totalPrice
          //     this.dataForm.isFinished = data.orders.isFinished
          //     this.dataForm.qrCodeUrl = data.orders.qrCodeUrl
          //     this.dataForm.createTime = data.orders.createTime
          //     this.dataForm.updateTime = data.orders.updateTime
          //   }
          // })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs.dataForm.validate(async (valid) => {
        if (valid) {
          const order = {
            orderNo: this.dataForm.orderNo || undefined,
            orderId: this.dataForm.orderId,
            ticketId: this.dataForm.ticketId,
            visitorIdCard: this.dataForm.visitorIdCard,
            purchaseQuantity: this.dataForm.purchaseQuantity,
            totalPrice: this.dataForm.totalPrice,
            isFinished: this.dataForm.isFinished,
            qrCodeUrl: this.dataForm.qrCodeUrl,
            createTime: this.dataForm.createTime,
            updateTime: this.dataForm.updateTime
          }
          const { data } = await saveOrUpdateOrder(this.dataForm.orderNo, order)
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(data.msg)
          }

          // this.$http({
          //   url: `/orders/${!this.dataForm.orderNo ? 'save' : 'update'}`,
          //   method: 'post',
          //   data: {
          //     orderNo: this.dataForm.orderNo || undefined,
          //     orderId: this.dataForm.orderId,
          //     ticketId: this.dataForm.ticketId,
          //     visitorIdCard: this.dataForm.visitorIdCard,
          //     purchaseQuantity: this.dataForm.purchaseQuantity,
          //     totalPrice: this.dataForm.totalPrice,
          //     isFinished: this.dataForm.isFinished,
          //     qrCodeUrl: this.dataForm.qrCodeUrl,
          //     createTime: this.dataForm.createTime,
          //     updateTime: this.dataForm.updateTime
          //   }
          // }).then(({ data }) => {
          //   if (data && data.code === 0) {
          //     this.$message({
          //       message: '操作成功',
          //       type: 'success',
          //       duration: 1500,
          //       onClose: () => {
          //         this.visible = false
          //         this.$emit('refreshDataList')
          //       }
          //     })
          //   } else {
          //     this.$message.error(data.msg)
          //   }
          // })
        }
      })
    }
  }
}
</script>
